﻿using System;
using System.Web;

using log4net;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2008-06-14
	Status:		release	
	Version:	2.0.0
	Build:		20080614
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20080614:	Initial release.
	---------------------------------------------------------------------------	*/

namespace umlungu.engineX.blogX.WebsiteX {
	/// <summary>
	/// The WebsiteXCookies class is a utility class for handling cookies on Website
	/// </summary>
	public class WebsiteXCookies {
		#region Invisible properties
		private displayX UIPage;
		#endregion

		#region Constants
		private const string logid = "WebsiteXCookies";
		private const string COOKIES = "WebsiteX";
		private const string COOKIE_SKIN = "Skin";
		private const string COOKIE_TOKEN = "TokenX";
		private const string COOKIE_WIZARD = "Wizard";
		private const string DEFAULT_WIZARD = "yes";	// see WebsiteStartupX
		private const int COOKIE_EXPIRY_YEARS = 5;
		#endregion

		#region Visible properties
		public static readonly ILog Logger = LogManager.GetLogger(typeof(WebsiteXCookies));
		private string skin = "white";
		/// <summary>The skin color to apply to the interface</summary>
		/// <value>Description of the skin color</value>
		public string Skin {
			get { return skin; }
			set { skin = value; }
		}
		private string token = "";
		/// <summary>The saved token</summary>
		/// <value>A login token</value>
		public string Token {
			get { return token; }
			set { token = value; }
		}
		private string wizard = "yes";
		/// <summary>The wizard flag</summary>
		/// <value>A start-up wizard</value>
		public string Wizard {
			get { return wizard; }
			set { wizard = value; }
		}
		#endregion

		#region Constructors/Destructors
		/// <summary>Default constructor</summary>
		public WebsiteXCookies(displayX thispage) {
			UIPage = thispage;
			log4net.Config.XmlConfigurator.Configure();
		}
		#endregion

		#region Public methods
		/// <summary>
		/// Reads the cookies from the request
		/// </summary>
		/// <param name="check">Flag to indicate if token cookie check is to be done</param>
		public void Read(bool check) {
			Logger.Debug(String.Concat(logid, "_Read", (UIPage.Request.Cookies[COOKIES] != null).ToString()));
			readSkinCookie();
			readWizardCookie();
			if (check)
				if (readTokenCookie())
					checkToken();
		}
		/// <summary>
		/// Reads the wizard cookie from the request
		/// </summary>
		public string ReadWizard() {
			Logger.Debug(String.Concat(logid, "_ReadWizard", (UIPage.Request.Cookies[COOKIES] != null).ToString()));
			readWizardCookie();
			return wizard;
		}
		/// <summary>
		/// Writes the cookies to the response
		/// </summary>
		public void Write(bool resetToken) {
			if (resetToken)
				token = "";
			Write();
		}
		/// <summary>
		/// Writes the cookies to the response
		/// </summary>
		public void Write() {
			HttpCookie skinCookie = new HttpCookie(COOKIE_SKIN);
			skinCookie.Value = skin;
			skinCookie.Expires = DateTime.Now.AddYears(COOKIE_EXPIRY_YEARS);
			UIPage.Response.Cookies.Add(skinCookie);

			Logger.Debug(String.Concat(logid, "_Write", "::token:", token));

			HttpCookie tokenCookie = new HttpCookie(COOKIE_TOKEN);
			tokenCookie.Value = token;
			tokenCookie.Expires = DateTime.Now.AddYears(COOKIE_EXPIRY_YEARS);
			UIPage.Response.Cookies.Add(tokenCookie);

			WriteWizard();
		}
		/// <summary>
		/// Writes the wizard cookie to the response
		/// </summary>
		public void WriteWizard() {
			HttpCookie wizardCookie = new HttpCookie(COOKIE_WIZARD);
			wizardCookie.Value = wizard;
			wizardCookie.Expires = DateTime.Now.AddYears(COOKIE_EXPIRY_YEARS);
			UIPage.Response.Cookies.Add(wizardCookie);
		}
		#endregion
		
		#region Protected methods
		#endregion

		#region Private utility methods

		private void readSkinCookie() {
			if (UIPage.Request.Cookies[COOKIE_SKIN] != null) {
				skin = UIPage.Request.Cookies[COOKIE_SKIN].Value;
				UIPage.UserProfile.Add(COOKIE_SKIN, skin);
			}
			Logger.Debug(String.Concat(logid, "_readSkinCookie::", "skin:", skin));
		}

		private bool readTokenCookie() {
			if (UIPage.Request.Cookies[COOKIE_TOKEN] != null) {
				token = UIPage.Request.Cookies[COOKIE_TOKEN].Value;
				UIPage.UserProfile.Add(COOKIE_TOKEN, token);
			}
			Logger.Debug(String.Concat(logid, "_readTokenCookie::", "token:", token));
			return (token != "");
		}

		private void readWizardCookie() {
			if (UIPage.Request.Cookies[COOKIE_WIZARD] != null) {
				wizard = UIPage.Request.Cookies[COOKIE_WIZARD].Value;
				UIPage.UserProfile.Add(COOKIE_WIZARD, wizard);
			}
			wizard = (wizard != "")? wizard : DEFAULT_WIZARD;
			Logger.Debug(String.Concat(logid, "_readWizardCookie::", "wizard:", wizard));
		}

		private void checkToken() {
			WebsiteXPassport broker = new WebsiteXPassport(UIPage); 
			broker.Process("token");
		}
		#endregion
	}
}
